import { fromJS } from 'immutable';
import * as constants from './constants';

const defaultState = fromJS({
  taskId: undefined,
  briefConfig: [],
  briefWebConfig: [],
  oriTable: [],//源数据库连接库表列表
  tarTable: [],//目标数据库连接库表列表
  column: [],//数据库输入字段
  columnFamily: [],
  field: [],//api输入字段
  inputField: [],//源数据库字段配置
  outputField: [],

  calcType: [],
  calcValueType: [],
  //字段合并
  mTable: [],
  mColumnFamily: [],
  mField: [],
});


const clearField = (state,action) => {
  return state.merge({
    column: action.column,
    columnFamily: action.columnFamily,
    field: action.field,
  })
}

const getInputField = (state,action) => {
  return state.merge({
    inputField: action.inputField,
    outputField: action.outputField,
  })
}

const clearMergeField = (state,action) => {
  return state.merge({
    mColumnFamily: action.mColumnFamily,
    mField: action.mField,
  })
}

export default(state=defaultState, action) => {
  switch(action.type){
    case constants.BUILD_TASK:
      return state.set('taskId',action.taskId);
    case constants.GET_BRIEF_CONFIG:
      return state.set('briefConfig',action.briefConfig);
    case constants.GET_BRIEF_WEB_CONFIG:
      return state.set('briefWebConfig',action.briefWebConfig);
    case constants.GET_ORIGIN_TABLE:
      return state.set('oriTable',action.table);
    case constants.GET_TARGET_TABLE:
      return state.set('tarTable',action.table);
    case constants.GET_COLUMNS:
      return state.set('column',action.column);
    case constants.GET_COLUMN_FAMILY:
      return state.set('columnFamily',action.columnFamily);
    case constants.CLEAR_FIELD:
      return clearField(state,action);
    case constants.GET_FIELD:
      return state.set('field',action.field);
    case constants.GET_INPUT_FIELD:
      return getInputField(state,action);
    case constants.GET_OUTPUT_FIELD:
      return state.set('outputField',action.outputField);

    case constants.GET_CALC_TYPE:
      return state.set('calcType',action.calcType);
    case constants.GET_CALC_VALUE_TYPE:
      return state.set('calcValueType',action.calcValueType);

    case constants.CLEAR_MERGE_FIELD:
      return clearMergeField(state,action);
    case constants.GET_MERGE_TABLE:
      return state.set('mTable',action.mTable);
    case constants.GET_MERGE_FIELD:
      return state.set('mField',action.mField);
    case constants.GET_MERGE_COLUMN_FAMILY:
      return state.set('mColumnFamily',action.mColumnFamily);
    default: return state;
  }
}